﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MediaUtility
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        delegate int AddHandler(int a, int b);

        private void button2_Click(object sender, EventArgs e)
        {
            string taskId = Guid.NewGuid().ToString("N");
            Trace.WriteLine("===== 异步调用 AsyncInvokeTest =====");
            AddHandler handler = new AddHandler(Add);

            //IAsyncResult: 异步操作接口(interface)
            //BeginInvoke: 委托(delegate)的一个异步方法的开始
            IAsyncResult result = handler.BeginInvoke(1, 2, new AsyncCallback(JobBack), taskId);

            Trace.WriteLine("继续做别的事情。。。");

            //异步操作返回
            //Console.WriteLine(handler.EndInvoke(result));
            //Console.ReadKey();
            //using (MediaAssistant info = new MediaAssistant(textBox1.Text))
            //{
            //    string tmpStr = info.GetInfo(Enum.StreamKind.Video, 0, "");
            //}
        }

        private int Add(int a, int b)
        {
            Trace.WriteLine("开始计算：" + a + "+" + b);
            Thread.Sleep(3000); //模拟该方法运行三秒
            Trace.WriteLine("计算完成！");
            return a + b;
        }

        private void JobBack(IAsyncResult result)
        {
            AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
            Trace.WriteLine(handler.EndInvoke(result));
            Trace.WriteLine(result.AsyncState);
        }
    }
}
